package com.example.controller;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.AutoLog;
import com.example.common.JwtTokenUtils;
import com.example.common.Result;
import com.example.common.UserHolder;
import com.example.domain.entity.Account;
import com.example.service.AdminService;
import com.example.service.UserService;
import com.example.service.EmpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
public class WebController {
    
    private static final Logger log = LoggerFactory.getLogger(WebController.class);


    @Resource
    private AdminService adminService;


    @Autowired
    private EmpService empService;
    
    @Autowired
    private UserService userService;
    
    /*
     * 登录
     */
    @PostMapping("/login")
    @AutoLog("登录该系统")
    public Result login(@RequestBody Account account, @RequestParam String key, HttpServletRequest request) {
        if (ObjectUtil.isEmpty(account.getUsername()) || ObjectUtil.isEmpty(account.getPassword())
                || ObjectUtil.isEmpty(account.getRole())) {
            return Result.error("账号或者密码为空");
        }
        if ("ADMIN".equals(account.getRole())) {
            account = adminService.login(account, key, request);
        }
        if ("EMP".equals(account.getRole())) {
            account = empService.login(account, key, request);
        }
        if ("USER".equals(account.getRole())) {
            account = userService.login(account, key, request);
        }
        return Result.success(account);
    }
    
    /**
     * 学生注册
     *
     * @param account
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody Account account) {
        if (ObjectUtil.isEmpty(account.getUsername()) || ObjectUtil.isEmpty(account.getPassword())
                || ObjectUtil.isEmpty(account.getRole())) {
            return Result.error("账号或者密码为空");
        }
        
        if ("USER".equals(account.getRole())) {
            userService.register(account);
        }
        return Result.success();
    }
    
    @GetMapping("/getUser")
    public Result findAll() {
        Account account = JwtTokenUtils.getCurrentUser();
        return Result.success(account);
    }
    
    @PostMapping("/updatePassword")
    @AutoLog("修改个人密码，退出系统")
    public Result updatePassword(@RequestBody Account account) {
        if (ObjectUtil.isEmpty(account.getUsername()) || ObjectUtil.isEmpty(account.getPassword())
                || ObjectUtil.isEmpty(account.getRole())) {
            return Result.error("账号或者密码为空");
        }
    
        if ("ADMIN".equals(account.getRole())) {
            adminService.updatePassword(account);
        }
        if ("EMP".equals(account.getRole())) {
            empService.updatePassword(account);
        }
        if ("USER".equals(account.getRole())) {
            userService.updatePassword(account);
        }
        return Result.success();
    }
    
    @PostMapping("/updatePersonal")
    @AutoLog("修改个人信息")
    public Result updatePersonal(@RequestBody Account account) {
        if (ObjectUtil.isEmpty(account.getName())) {
            return Result.error("姓名不能为空");
        }
        if ("ADMIN".equals(account.getRole())) {
            adminService.updatePersonal(account);
        }
        if ("EMP".equals(account.getRole())) {
            empService.updatePersonal(account);
        }
        if ("USER".equals(account.getRole())) {
            userService.updatePersonal(account);
        }
        return Result.success();
    }
    
    @PostMapping("/out")
    @AutoLog("退出该系统")
    public Result out(@RequestBody Account account) {
        UserHolder.removeUser();
        return Result.success(account);
    }
    
}
